﻿using FarseerPhysics;
using FarseerPhysics.Dynamics;
using JilyGameFramework.Classes;
using JilyGameFramework.Datas;
using JilyGameFramework.Manager;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace JilyGameFramework.Controls
{
    public class Control : GameScreen
    {

        private Rectangle bounds;
        public virtual Rectangle Bounds
        {
            get { return bounds; }
            set { bounds = value; }
        }

        protected Texture2D BackGroundTexture;
        private string backGroundImage;
        public string BackGroundImage
        {
            get { return backGroundImage; }
            set { backGroundImage = value; }
        }

        public bool IsPress { get; protected set; }

        public Control(Rectangle bounds, string image)
            : base()
        {
            this.BackGroundImage = image;
            this.Bounds = bounds;
        }

        public override void LoadContent()
        {
            base.LoadContent();

            BackGroundTexture = ScreenManager.Content.Load<Texture2D>(this.BackGroundImage);

            this.ScreenManager.Input.OnMouseAction += Input_OnMouseAction;
            this.Bounds = ConvertViewDefaultToReal(this.Bounds);
        }

        public override void UnLoadContent()
        {
            base.UnLoadContent();
        }

        public bool IsHit(Vector2 position)
        {
            return this.Bounds.Contains((int)position.X, (int)position.Y);
        }

        void Input_OnMouseAction(object sender, MouseDataArgs e)
        {

            //Fixture savedFixture =   World.TestPoint(ConvertUnits.ToSimUnits(e.Point));

            switch (e.Type)
            {
                case MouseTpye.Down:
                    {
                        MouseDown(e.Point);
                        break;
                    }

                case MouseTpye.Move:
                    {
                        MouseMove(e.Point);
                        break;
                    }

                case MouseTpye.Up:
                    {
                        MouseUp(e.Point);
                        break;
                    }
            }
        }

        protected virtual void MouseDown(Vector2 point)
        {
            if (IsHit(point))
            {
                this.IsPress = true;
            }
        }

        protected virtual void MouseMove(Vector2 point)
        {

        }

        protected virtual void MouseUp(Vector2 point)
        {
            this.IsPress = false;
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Draw(gameTime);
            ScreenManager.SpriteBatch.Begin();
            DrawView();
            ScreenManager.SpriteBatch.End();
        }

        public virtual void DrawView()
        {
            ScreenManager.SpriteBatch.Draw(BackGroundTexture, this.Bounds, Color.White);
        }

        public Rectangle ConvertViewDefaultToReal(Rectangle bounds)
        {
            var spanx = this.ScreenManager.SpanWidth;
            var spany = this.ScreenManager.SpanHeight;
            return new Rectangle((int)(bounds.X * spanx), (int)(bounds.Y * spany), (int)(bounds.Width * spanx), (int)(bounds.Height * spany));
        }
    }
}
